Script SleepCheck
(x)

U4 t0, t1;
U4 gap = x;
t0 = GetTick();
Sleep(gap);
PrintLn(GetTick() - t0);

EndScript

Script GenericSet
(ename, key, value)
I4 ret;
GenericSet@Secs(:ename, key, value, ret);
PrintLn(ret);
EndScript

Script SetGlobal
(name, value)
:name = value;
EndScript

Script Set
(x, key, value)
I4 ret;
GenericSet@Secs(:x, key, value, ret);
if (ret)
{
	PrintLn("Failed to set ", key, " to ", value);
}
else
{
	PrintLn("OK");
}
EndScript

Script SendS1F3
(x)

U4 n = GetCount(Parameter) - 1;
U4 ret, loop = 0;

SecsMessage@Secs m, rmsg;
SetSFW@Secs(m, 1, 3, 1);
Set@Secs(m, "List", n, 0, 0, ret);
while (loop < n)
{
	Set@Secs(m[loop], "U4", 1, 0, ToU4(Parameter[loop + 1]), ret); 
	Inc(loop);
}

PrintLn(m);			//to check how the message was formed

if (SendAndWaitEx@@Secs(:x, m, rmsg, QS_EID()))
{
	PrintLn("SendAndWait failed");
}
else
{
	PrintLn(rmsg);
}

EndScript

Script GetSecsMessage
(name, m)

String s;
U4 ret;

if (GetResource(name, s))
{
	PrintLn (ScriptName(), "Failed to GetResource ", name);
	return -1;
}

Parse@Secs(m, s, ret);
if (ret)
{
	PrintLn (ScriptName(), " failed to parse");
	PrintLn (s);
	return -1;
}

return 0;

EndScript


Script EntryScript

String s;
U4 ret;

LibLoad(LinWin("./libQSECS.so", "QSECS.dll"), "Secs", ret);
if (ret && ret != 4)		//for now at development stage
{
	PrintLn ("LibLoad for QSECS gives ", ret);
	return;
}
GCreate("SimTool", 1);			//A global flag to decide which S1F14 to use in reply
GCreate("SentCount", 0);
EndScript

Script SetSimTool
(i)
:SimTool = i;
EndScript

Script SendLinkTest
(name)
U4 ret;
SendLinkTest@Secs(:name, ret);
EndScript

Script SetLinkTestPeriod
(name, p)
U4 period;
period = p;
SetLinkTestPeriod@Secs(:name, period);
EndScript

Script StartCommHost
(name, port)
if (MapCheck(Global, name))
{
	PrintLn(name, " is already defined in Global space");
	return;
}

GCreateEx2(name, "SECSI", "Secs");
SetPortName@Secs(:name, port);
SetMasterEquipment@Secs(:name, 0, 0);		//Master = 0, Equipment = 0
StartSECSI(name);

EndScript

Script StartCommTool
(name, port)

if (MapCheck(Global, name))
{
	PrintLn(name, " is already defined in Global space");
	return;
}

GCreateEx2(name, "SECSI", "Secs");
SetPortName@Secs(:name, port);
SetMasterEquipment@Secs(:name, 1, 1);		//Master = 1, Equipment = 1;
StartSECSI(name);

EndScript

Script StartTCPHost
(name, ip, port)

I4 ret;

if (MapCheck(Global, name))
{
	PrintLn(name, " is already defined in Global space");
	return;
}

GCreateEx2(name, "SECSI", "Secs");
GenericSet@Secs(:name, "Remote IP", "127.0.0.1", ret);
GenericSet@Secs(:name, "Remote port", "8000", ret);
GenericSet@Secs(:name, "Comm type", "1", ret);
SetMasterEquipment@Secs(:name, 0, 0);		//Master = 0, Equipment = 0
StartSECSI(name);

EndScript

Script StartTCPTool
(name, port)

I4 ret;
if (MapCheck(Global, name))
{
	PrintLn(name, " is already defined in Global space");
	return;
}

GCreateEx2(name, "SECSI", "Secs");
GenericSet@Secs(:name, "Local port", "8000", ret);
GenericSet@Secs(:name, "Comm type", "1", ret);
GenericSet@Secs(:name, "Connect mode", "0", ret);
SetMasterEquipment@Secs(:name, 1, 1);		//Master = 1, Equipment = 1;
StartSECSI(name);

EndScript


Script StartActive
(name, ip, port)

U4 ret;
I4 Port;

if (MapCheck(Global, name))
{
	PrintLn(name, " already exist in global space");
	return;
}
GCreateEx2(name, "HSMS", "Secs");

SetRemoteIP@Secs(:name, ip, ToU4(port));
//SetConnectMode@Secs(:name, 1);
GenericSet@Secs(:name, "Connect mode", "1", ret);
StartEngine(name);
EndScript

Script StartPassive
(name, port)

U4 ret;
I4 Port;

if (MapCheck(Global, name))
{
	PrintLn(name, " is already defined in Global space");
	return;
}

GCreateEx2(name, "HSMS", "Secs");

Port = port;
SetLocalIP@Secs(:name, "0.0.0.0", ToU4(port));
//SetConnectMode@Secs(:name, 0);		//set to the passive entity
GenericSet@Secs(:name, "Connect mode", "0", ret);

StartEngine(name);
EndScript

Script StartEngine
(name)
I4 ret;
String tmp;
//SetHandler@Secs(:name, "Handler", 1);			//0 for parallel call, 1 for serial call

SetName@Secs(:name, name);
GenericSet@Secs(:name, "Handler", "Handler", ret);
GenericSet@Secs(:name, "Max HSMS length", StrAssign(tmp, 0xFFFFFFFFU4), ret);
GenericSet@Secs(:name, "T3", StrAssign(tmp, 90), ret);
GenericSet@Secs(:name, "Device ID", StrAssign(tmp, 0), ret);
SetLogOption@Secs(:name, StrAssign(tmp, "Log\\", name, "_%Y%M%D_%h%m.txt"), "%T ", 0, 0U8, 3);		//Use timestamp, and log everything, with MaxLogFile set, and no time roll over
SetLogOn@Secs(:name, 1);			//Enable the log

SetLogCloseName@Secs(:name, StrAssign(tmp, "Log\\", name, "_%Y%M%D_%h%m.log"));
Start@Secs(:name);
EndScript

Script StartSECSI
(name)
I4 ret;
String tmp;
SetHandler@Secs(:name, "Handler", 1);			//0 for parallel call, 1 for serial call
SetName@Secs(:name, name);
SetLogOption@Secs(:name, StrAssign(tmp, "Log\\", name, "_%Y%M%D_%h%m.txt"), "%T ", 0, 0U8, 3);		//Use timestamp, and log everything, with MaxLogFile set, and no time roll over
SetLogOn@Secs(:name, 1);			//Enable the log
SetLogCloseName@Secs(:name, StrAssign(tmp, "Log\\", name, "_%Y%M%D.log"));
Start@Secs(:name);
EndScript

Script GType
(x)
PrintLn(GetType(:x));
PrintLn(GetActualType(:x));

EndScript

Script Stop
(name)
if (MapCheck(Global, name))
{
	StopAndWait@Secs(:name);
	GDelete(name);
	PrintLn(name, " is stopped");
}
else
{
	PrintLn(name, " is not defined in Global space");
}
EndScript

Script Stop2
(name)
if (MapCheck(Global, name))
{
	Stop@Secs(:name);
	PrintLn(name, " is stopped");
}
else
{
	PrintLn(name, " is not defined in Global space");
}
EndScript


Script Destroy
(name)
if (MapCheck(Global, name))
{
	GDelete(name);
	PrintLn(name, " is stopped");
}
else
{
	PrintLn(name, " is not defined in Global space");
}
EndScript

Script state
(x)
PrintLn(Status@@Secs(:x));
EndScript

Script ShowMessage
(msg)
PrintLn(msg);
EndScript

Script Parse
(name)
SecsMessage@Secs m;
GetSecsMessage(name, m);
PrintLn(m);
EndScript

Script Handler
(engine, enginename, type, new, old)		//State change call back
(engine, enginename, type, opaque, sb, msg, did, sfw)	//SECS Message received
(engine, enginename, type, logdata)			//log callback, type = 7

U4 ret, n, loop, tmp;
String s, t;
Object o;
SecsMessage@Secs m;
F4 f4;
U4 f, stream, w;

switch (type)
{
case 0
	PrintLn (enginename, " network state change, new state is ", Parameter[3], ", old state is ", old);
	break;
	
case 1
	PrintLn (enginename, "] ", sfw, " is received, SB is ", sb, ", Device ID is ", did, ", Opaque is ", opaque);
	switch (sfw)
	{
	case "S1F2"
		//PCall("ShowMessage", msg);
		break;

	case "S1F1W"
		if (:SimTool)
		{
			Send_Reply(engine, "S1F2_Tool", sb);
		}
		else
		{
			Send_Reply(engine, "S1F2_Host", sb);
		}
		break;
		
	case "S1F3W"
		SetSFW@Secs(m, 1, 4, 0);
		n = GetCount(msg[]);
		Set@Secs(m, "List", n, 0, 0, ret);
		loop = 0;
		while (loop < n)
		{
			f4 = msg[loop][0] * 1.5;
			Set@Secs(m[loop], "F4", 1, 0, f4, ret);
			Inc(loop);
		}
		Send_Reply(engine, m, sb);
		break;
			
	case "S1F13W"
		if (:SimTool)
		{
			GetSecsMessage("S1F14_Tool", m);
		}
		else
		{
			GetSecsMessage("S1F14_Host", m);
		}
		SendSecsMsg@Secs(engine, m, 0U0, sb, ret);
		break;
			
	case "S2F33W"
	case "S2F35W"
	case "S2F37W"
		GetSecsMessage("Generic_OK", m);
		GetFunction@Secs(msg, f);
		SetFunction@Secs(m, f + 1);
		SendSecsMsg@Secs(engine, m, 0U0, sb, ret);
		break;
		
	case "S63F99W"
	case "S99F99W"
		GetSFW@Secs(msg, stream, f, w);
		SetSFW@Secs(m, stream, f + 1, 0);
		SecsItemCopy@Secs(m, msg, ret);
		if (ret)
		{
			PrintLn("Failed to perform SecsItemCopy@Secs");
		}
		else
		{
			Send_Reply(engine, m, sb);
		}
		break;
	}
	break;
	
case 2
	PrintLn (enginename, "] message sent, SB is ", sb, ", opaque is ", opaque);
	if ((Inc(:SentCount) % 10000) == 0)
	{
		PrintLn("SentCount is ", :SentCount);
	}
	break;
	
case 3
	PrintLn (enginename, " message sent failed, SB is ", sb, ", opaque is ", opaque);
	break;
	
case 4
	PrintLn (enginename, " fail to receive reply, SB = ", sb, ", opaque = ", opaque);
	break;

case 7
	PrintLn (logdata);
	break;
	
default
	PrintLn (enginename, " unexpected type at Handler ", type);
	break;
}
EndScript


Script Send
(name, msg)
XSend(name, msg, 1);
EndScript

Script SendAndWait
(x, msg)

I4 ret;
SecsMessage@Secs rmsg, m;
I4 wbit;

if (GetSecsMessage(msg, m))
{
	return -1;
}
	
GetWaitBit@Secs(m, wbit);
ret = SendAndWaitEx@@Secs(:x, m, rmsg, QS_EID());

if (ret)
{
	PrintLn("SendAndWaitEx failed ", ret);
}
else
{
	if (wbit)
	{
		PrintLn(rmsg);
	}
}

EndScript

Script XSendAndWait
(x, msg, count)

I4 ret;
U0 n = count;

SecsMessage@Secs rmsg, m;
I4 wbit;

if (GetSecsMessage(msg, m))
{
	return -1;
}
	
GetWaitBit@Secs(m, wbit);

while (n)
{
	Dec(n);	
	ret = SendAndWaitEx@@Secs(:x, m, rmsg, QS_EID());

	if (ret)
	{
		PrintLn("SendAndWaitEx failed ", ret);
	}
	else
	{
		if (n % 100000 == 0)
		{
			PrintLn("Marker at ", n);
		}
		/*
		if (wbit)
		{
			PrintLn(rmsg);
		}
		else
		{
			PrintLn("SendAndWait on message without wait bit is good");
		}
		*/
	}
}

EndScript

Script XSend
(name, msg, count)
U4 ret, len, sb;
U4 n;
SecsMessage@Secs m;

n = count;
if (GetSecsMessage(msg, m))
{
	return -1;
}

while (n)
{
	Dec(n);
	SendSecsMsg@Secs(:name, m, 0U0, sb, ret);
	Sleep(0);
	
	if (ret)
	{
		PrintLn("Failed to send message");
	}
}
EndScript

Script XXSend
(name, size, line, count)
U4 ret, len, sb;
U4 n;
U4 loop = 0;
SecsMessage@Secs m;
String s;

StrRepeat(s, "A", ToU4(size));

Set@Secs(m, "L", ToU4(n), 0, 0, ret);

n = line;
while (loop < n)
{
	Set@Secs(m, "A", n, 0, ToStream(s), ret);
	Inc(loop);
}

n = count;
while (n)
{
	Dec(n);
	SendSecsMsg@Secs(:name, m, 0U0, sb, ret);
	Sleep(0);
	
	if (ret)
	{
		PrintLn("Failed to send message");
	}
}
EndScript

Script Send_Reply
(engine, msg, sb)

SecsMessage@Secs m;
U4 ret;
if (GetActualType(msg) == "SecsMessage@Secs")
{
	SendSecsMsg@Secs(engine, msg, 0U0, sb, ret);
}
else
{
	if (GetSecsMessage(msg, m) == 0)
	{
		SendSecsMsg@Secs(engine, m, 0U0, sb, ret);
	}
	else
	{
		PrintLn("Failed to GetSecMessage for ", msg);
		return;
	}
}

EndScript

Script Check_SECSII
(name)
SecsMessage@Secs m;
if (GetSecsMessage(name, m) == 0)
{
	PrintLn(m);
}
EndScript

Script Show
(x)
SecsMessage@Secs m;
GetSecsMessage(x, m);
PrintLn(m);
EndScript

Resource S88F1W
S88F1 W
<L
	<F8 123.456>
	<F4 456.123>
>.
EndResource

Script XStartActive
(name, ip, port, n)

U4 loop, count;
String s;
loop = 0;
while (loop < StrToX(n, "U4"))
{
	StartActive(StrAssign(s, name, loop), ip, port + loop);
	Inc(loop);
}
EndScript

Script XStartPassive
(name, port, n)

U4 loop;
String s;
loop = 0;
while (loop < StrToX(n, "U4"))
{
	StartPassive(StrAssign(s, name, loop), port + loop);
	Inc(loop);
}
EndScript

Script XStop
(name, n)
U4 loop;
String s;
loop = 0;
while (loop < StrToX(n, "U4"))
{
	Stop(StrAssign(s, name, loop));
	Inc(loop);
}
EndScript

Resource Hello
S1F1 W.
EndResource

Resource HelloNoWait
S1F1.
EndResource

Resource S1F3
S1F3 W
<L
	<U4 500102>
>.
EndResource

Resource S1F2_Host
S1F2
<L>.
EndResource

Resource S1F2_Tool
S1F2
<L
	<A "MDLN_1">
	<A "VERSION_1">
>.
EndResource

Resource S1F14_Tool
S1F14
<L
	<B 00>
	<L
		<A "MDLN_1">
		<A "VERSION_1">
	>
>.
EndResource

Resource S1F14_Host
S1F14
<L
	<B 00>
	<L>
>.
EndResource

Resource Online
S1F17 W.
EndResource

Resource EstComm

S1F13 W
<L>.

EndResource


Resource PurgeSpooled
S6F23 W
<U1 1 >.
EndResource

Resource TransmitSpooled
S6F23 W
<U1 0 >.
EndResource

Resource StopSpool
S2F15 W
<L 
	<L
		<U4 4010>
		<BOOLEAN 0>
	>

>.
EndResource


Resource ChangeSpool
S2F15 W
<L 
	<L
		<U4 4010>
		<BOOLEAN 1>
	>

>.
EndResource

Resource GetSpool
S2F13 W
<L
	<U4 4010>
>.
EndResource



Resource Blank
S1F9
<L>.
EndResource

Script TInsert
SecsMessage@Secs m, m2;
I4 ret;
GetSecsMessage("Blank", m);
GetSecsMessage("Stuff", m2);



InsertBack@Secs(m[], "L", 4, 0, 0, ret);

Sys_Debug(m);
Sys_Debug(m2);

Sys_Debug(m[0][1]);
Sys_Debug(m2[]);

//PrintLn(m);
//PrintLn(m2);
//PrintLn(m[1]);
//PrintLn(m2[]);

m[0][1] = m2[];
PrintLn(m);
EndScript

Resource Stuff
S1F9
<L
	<A "Hello">
	<A "World">
	<I4 123>
>.
EndResource

Resource Generic_OK
S2F34
<B 00>.
EndResource

Script ContinuosSend
(name, frequency, msg)
F8 x = frequency;			//convert to F8
U8 gap, elapse;
SecsMessage@Secs m, rmsg;
U8 t0, t1, t2;			//to calculate the time between 2 message send
U4 stime;		//sleep time

if (frequency > 1000)
{
	gap = 0;			//gap is the time lapse within each primary message
}
else
{
	gap = 10000000F8 / frequency;
}

if (GetSecsMessage(msg, m))
{
	PrintLn("Error: Failed to get resource on ", msg);
	return;
}

PrintLn("ContinuosSend for ", name, ",  with gap = ", gap, " ms.  SECS message is\x0A", m);

PrintLn("Starting sending primary message on ", name, " with frequency = ", frequency);

t0 = CurrentFT();		//get time stamp
Again:		//label for goto (which will create infinite loop)

//use this for synchronuos send
if (SendAndWaitEx@@Secs(:name, m, rmsg, QS_EID()))
{
	PrintLn("Error: SendAndWait@Secs failed");
}

/*
//use this for asynchronuos send
if (SendSecsMsg@Secs(:name, m, 0U0, sb, ret))
{
	PrintLn("Error: SendSecsMsg@Secs failed");
}
*/


t1 = CurrentFT();

if (t1 > t0)			//this check is needed because t0 could be calculated
{
	elapse = t1 - t0;
	if (elapse < gap)
	{
		stime = (gap - elapse) / 10000;
		t0 = t0 + gap;
		Sleep(stime);
	}
	else
	{
		//no need to sleep, already expired
		t0 = CurrentFT();
	}
}
else
{
	stime = (gap + t0 - t1) / 10000;
	t0 = t0 + gap;
	Sleep(stime);
}

goto Again;

EndScript

Resource GetAllSVID

S1F11 W
<L
	<A>
>.

EndResource

Script CW
StartPassive("X", 8000);
StartActive("Y", "127.0.0.1", 8000);
Sleep(1000);
Send("X", "Hello");
EndScript

Script Sean
StartTCPHost("X", "127.0.0.1", "8000");
StartTCPTool("Y", "8000");
EndScript


Script TestPr
StartActive("PR22test","172.18.38.105","9189");
Again:
Send("PR22test" ,"testpr");
Sleep(2000);
goto Again;
EndScript

Resource buffer
S1F3 W
<L
	<U8 1510003100000000>
	<U8 1510003200000000>
>.
EndResource

Resource UnlinkAllReport
S2F35 W
<L
	<U4 0>
	<L>
>.
EndResource

Resource DeleteAllReport
S2F33 W
<L
	<U4 0>
	<L>
>.
EndResource


Resource DisableAllEvent
S2F37 W
<L
  <BOOLEAN F>
  <L>
>.
EndResource


Resource EnableAllEvent
S2F37 W
<L
	<BOOLEAN T>
	<L>
>.
EndResource



Resource DisableEvent
S2F37 W
<L
	<Boolean F>
	<L
   		<U4 1502040000>
		<U4 1502080000>
	>
>.
EndResource

Resource UnlinkReport
S2F35 W
<L
	<U2 1>
   	<L
    	<L
    		<U4 1502040000>
			<L>
    	>
		<L 
			<U4 1502080000>
			<L>
		>
	>
>.
EndResource

Resource UndefineReport
S2F33 W
<L
    <U2 1> 
    <L
		
    	<L
    		<U4 1502040000>
			<L>
    	>
		<L 
			<U4 1502080000>
			<L>
		>
	>
>.
EndResource

Resource DefineReport
S2F33 W
<L
	<U2 1>
   	<L
    	
    	<L
    		<U4 1502040000>
			<L
				<U4 2390100000>
				<U4 2390130000>
				<U4 2390140000>
				<U4 2390080000>
			>
    	>
		<L 
			<U4 1502080000>
			<L
				<U4 2390100000>
				<U4 2390130000>
				<U4 2390140000>
				<U4 2390080000>
			>
		>
	>
>.
EndResource

Resource LinkReport
S2F35 W
<L
	<U2 1>
   	<L
    	<L
    		<U4 1502040000>
			<L
				<U4 1502040000>
			>
    	>
		<L 
			<U4 1502080000>
			<L
				<U4 1502080000>
			>
		>
	>
>.
EndResource

Resource EnableEvent
S2F37 W
<L
	<Boolean T>
	<L
   		<U4 1502040000>
		<U4 1502080000>
	>
>.
EndResource


Resource version
 S1F13 W
  <L [0]
  >.

EndResource

Resource a
S2F37 W
<L
  <BOOLEAN T>
  <L
	<U8 1500800000>
  >
>.
EndResource

Resource b
S2F37 W
<L
  <BOOLEAN T>
  <L
	<U8 1500810000>
  >
>.
EndResource

Resource c
S2F37 W
<L
  <BOOLEAN T>
  <L
	<U8 1502040000>
  >
>.
EndResource

Resource d
S2F37 W
<L
  <BOOLEAN T>
  <L
	<U8 1502070000>
  >
>.
EndResource

Resource e
S2F37 W
<L
  <BOOLEAN F>
  <L
	<U8 1502110000>
  >
>.
EndResource

Resource S1F13
S1F13 W
<L>.
EndResource


Resource bbb
S1F3 W
<L
	<U8 2301307101000000 >
	<U8 2301307102000000 >
	<U8 2301307103000000 >
	<U8 2301307104000000 >
	<U8 2301307105000000 >
	<U8 2301307106000000 >
	<U8 2301307107000000 >
	<U8 2301307108000000 >
>.
EndResource


Resource s5f3
S5F3 W
<L
	<B 0x80>
	<U4>
>.
EndResource


Resource s1f11
S1F11 W
<L
	<A>
>.
EndResource


Resource aa
S2F37 W
<L
  <BOOLEAN F>
  <L
  >
>.
EndResource

Resource bb
S2F35 W
<L
	<U4 0>
	<L>
>.
EndResource

Resource cc
S2F33 W
<L
	<U4 0>
	<L>
>.
EndResource

Resource dd
S2F33 W
<L
	<U2 1>
   	<L
    	
    	<L
    		<U4 155>
			<L
				<U4 2004>
				<U4 6000>
				<U4 6001>
				<U4 6002>
				<U4 6003>
				<U4 6004>
				<U4 6005>
				<U4 2055>
			>
    	>
	>
>.
EndResource

Resource ee
S2F35 W
<L
	<U2 1>
   	<L
    	<L
    		<U4 108>
			<L
				<U4 155>
			>
    	>
	>
>.
EndResource

Resource ff
S2F37 W
<L
	<Boolean T>
	<L
   		<U4 108>
	>
>.
EndResource